package app.view;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import app.model.EmailListItem;

/**
 * @author MICHAEL
 *
 */
public class AttachmentsListActivity extends ListActivity {
	
	private AtachmentsAdapter _listAdapter;
	private ArrayList<String> _attachments;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atachments_list);
        
        // read the email data to display
        Bundle bud = getIntent().getExtras();
        _attachments = bud.getStringArrayList("atachments");
     		 
        _listAdapter = new AtachmentsAdapter(this, R.layout.attachment_list_item, _attachments);
        setListAdapter(_listAdapter);
        
        // enable context menu
        registerForContextMenu(getListView());
	}
	

	
	/**
	 * deletes chosen attachment
	 */
	public void Delete()
	{
		_attachments.remove(_toDeleteIndex);
		_listAdapter.notifyDataSetChanged();
	}
	
	
	
	private int _toDeleteIndex;
	
	@Override
	// show the context menu
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		
		_toDeleteIndex = ((AdapterContextMenuInfo)menuInfo).position;
		
		menu.setHeaderTitle("Operations");
		menu.add("Delete");
		menu.add("Cancel");
	
		menu.getItem(0).setOnMenuItemClickListener(delete_onMenuClick);
	}
	
	private OnMenuItemClickListener delete_onMenuClick = new OnMenuItemClickListener() {
		
		public boolean onMenuItemClick(MenuItem item) {
			
			AlertDialog ad = new AlertDialog.Builder(AttachmentsListActivity.this).create();  
			ad.setCancelable(false); // This blocks the 'BACK' button  
			ad.setMessage("Are you sure you want to delete this email? ");  
			
			ad.setButton("Cancel", new DialogInterface.OnClickListener() {  
			    public void onClick(DialogInterface dialog, int which) {  
			        dialog.dismiss();                      
			    }  
			});
			
			// when user choice "OK" then email will be deleted 
			ad.setButton2("OK", new DialogInterface.OnClickListener() {  
			    public void onClick(DialogInterface dialog, int which) {  
			    	
			    	Delete();
			        dialog.dismiss();                      
			    }  
			});
			
			ad.show(); 
			return false;
		}
	};
	
	
	
	/**
	 * ListView Adapter needed for defining custom list item appearance
	 */
	private class AtachmentsAdapter extends ArrayAdapter<String> {

		private ArrayList<String> items; // items source handler

		public AtachmentsAdapter(Context context, int textViewResourceId,
				ArrayList<String> items) {

			super(context, textViewResourceId, items);
			this.items = items;
		}

		/**
		 * When list is going to draw item then this method prepares its
		 * content. Data is read and converted into custom layout
		 * 
		 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
		 *      android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View v = convertView;
			if (v == null) {
				// loads xml layout into View
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.attachment_list_item, null);
			}

			// get widget handlers
			TextView txtAtach = (TextView) v.findViewById(R.id.txtItemAttach);

			// sets the widgets content
			txtAtach.setText(items.get(position));

			return v;
		}
	}

}
